package com.shop.cloud.common;

import javax.annotation.Resource; 

import org.springframework.beans.factory.config.AutowireCapableBeanFactory;

import com.egzosn.pay.common.api.PayConfigStorage;
import com.egzosn.pay.common.api.PayMessageHandler;
import com.egzosn.pay.common.api.PayMessageRouter;
import com.egzosn.pay.common.api.PayService;
import com.egzosn.pay.common.bean.MsgType;
import com.egzosn.pay.common.http.HttpConfigStorage;
import com.shop.cloud.common.handler.AliPayMessageHandler;
import com.shop.cloud.common.handler.WxPayMessageHandler;
import com.shop.cloud.common.interceptor.AliPayMessageInterceptor;
import com.shop.cloud.domain.pay.PayAccount;
import com.shop.cloud.domain.pay.PayType;

/**
 * 支付响应对象
 */
public class PayResponse {

	@Resource
	private AutowireCapableBeanFactory spring;

	private PayConfigStorage storage;

	private PayService service;

	private PayMessageRouter router;

	/**
	 * 初始化支付配置
	 * 
	 * @param apyAccount
	 *            账户信息
	 * @see PayAccount 对应表结构详情--》 /pay-java-demo/resources/apy_account.sql
	 */
	public void init(PayAccount apyAccount) {
		// 根据不同的账户类型 初始化支付配置
		this.service = apyAccount.getPayType().getPayService(apyAccount);
		this.storage = service.getPayConfigStorage();
		// 这里设置http请求配置
		// service.setRequestTemplateConfigStorage(getHttpConfigStorage());
		buildRouter(apyAccount.getPayId());
	}

	/**
	 * 获取http配置，如果配置为null则为默认配置，无代理,无证书的请求方式。 此处非必需
	 * 
	 * @param apyAccount
	 *            账户信息
	 * @return 请求配置
	 */
	public HttpConfigStorage getHttpConfigStorage(PayAccount apyAccount) {
		HttpConfigStorage httpConfigStorage = new HttpConfigStorage();
		/* 网路代理配置 根据需求进行设置 */
		// //http代理地址
		// httpConfigStorage.setHttpProxyHost("192.168.1.69");
		// //代理端口
		// httpConfigStorage.setHttpProxyPort(3308);
		// //代理用户名
		// httpConfigStorage.setHttpProxyUsername("user");
		// //代理密码
		// httpConfigStorage.setHttpProxyPassword("password");

		// 设置ssl证书路径
		httpConfigStorage.setKeystorePath(apyAccount.getKeystorePath());
		// 设置ssl证书对应的密码
		httpConfigStorage.setStorePassword(apyAccount.getStorePassword());
		return httpConfigStorage;
	}

	/**
	 * 配置路由
	 * 
	 * @param payId
	 *            指定账户id，用户多微信支付多支付宝支付
	 */
	private void buildRouter(Integer payId) {
		router = new PayMessageRouter(this.service);
		router.rule().async(false).msgType(MsgType.text.name()) // 消息类型
				.payType(PayType.aliPay.name()) // 支付账户事件类型
				.interceptor(new AliPayMessageInterceptor()) // 拦截器
				.handler(autowire(new AliPayMessageHandler(payId))) // 处理器
				.end().rule().async(false).msgType(MsgType.xml.name()).payType(PayType.wxPay.name()).handler(autowire(new WxPayMessageHandler(payId))).end();
	}

	private PayMessageHandler autowire(PayMessageHandler handler) {
		spring.autowireBean(handler);
		return handler;
	}

	public PayConfigStorage getStorage() {
		return storage;
	}

	public PayService getService() {
		return service;
	}

	public PayMessageRouter getRouter() {
		return router;
	}
}
